import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import datastructures.Cache;
import datastructures.Instruction;
import datastructures.Reservation_Station_Entry;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JScrollPane;
import javax.swing.JTable;


public class Simulator_View implements ActionListener {

	private JFrame frame;
	
	JPanel panel;
	JPanel panel_2;
	
	private JTextField immediate;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;
	private JTextField textField_8;
	private JTextField textField_9;
	private JTextField textField_10;
	private JTextField textField_11;
	private JTextField textField_12;
	private JTextField textField_13;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_14;
	private JTextField textField_15;
	private JTextField textField_16;
	private JTextField textField_17;
	private JTextField textField_18;
	private JTextField textField_19;
	
	JTextArea program;
	
	JComboBox comboBox_2;
	JComboBox comboBox_3;
	JComboBox comboBox_4;
	JComboBox comboBox_5;
	JComboBox comboBox_6;
	JComboBox comboBox_7;
	JComboBox comboBox_8;
	JComboBox comboBox_9;
	JComboBox instruction;
	JComboBox op1;
	JComboBox op2;
	JComboBox op3;
	
	JCheckBox checkBox;
	JCheckBox checkBox_1;
	JCheckBox checkBox_2;
	
	JButton btnDone;
	
	int numOfInst = 0;
	
	Cache cache;
	
	Simulator simulator = new Simulator();
	private JTextField startAddTextField;
	private JTextField addressTextField;
	private JTextField valueTextField;
	private JLabel lblNumberOfRob;
	private JTextField robText;
	private JLabel lblCacheOrganization;
	private JTextField l1AssociativityText;
	private JTextField l2AssociativityText;
	private JTextField l3AssociativityText;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Simulator_View window = new Simulator_View();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Simulator_View() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 600);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		panel = new JPanel();
		panel.setVisible(false);
		
		panel_2 = new JPanel();
		panel_2.setBounds(0, 0, 434, 562);
		frame.getContentPane().add(panel_2);
		panel_2.setLayout(null);
		
		JLabel label = new JLabel("Memory hierarchy :");
		label.setBounds(10, 11, 103, 23);
		panel_2.add(label);
		
		checkBox = new JCheckBox("L1");
		checkBox.setEnabled(false);
		checkBox.setSelected(true);
		checkBox.setBounds(10, 41, 103, 23);
		panel_2.add(checkBox);
		
		checkBox_1 = new JCheckBox("L2");
		checkBox_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			
				if(((JCheckBox)e.getSource()).isSelected()){
					
					checkBox_2.setEnabled(true);
					textField_5.setEnabled(true);
					textField_5.setEditable(true);
					textField_6.setEnabled(true);
					textField_6.setEditable(true);
					textField_7.setEnabled(true);
					textField_7.setEditable(true);
					comboBox_6.setEditable(true);
					comboBox_6.setEnabled(true);
					comboBox_7.setEditable(true);
					comboBox_7.setEnabled(true);
					textField_12.setEditable(true);
					textField_12.setEnabled(true);
					l2AssociativityText.setEditable(true);
					l2AssociativityText.setEnabled(true);
					
					
				}else{
					
					checkBox_2.setEnabled(false);
					textField_8.setEnabled(false);
					textField_8.setEditable(false);
					textField_9.setEnabled(false);
					textField_9.setEditable(false);
					textField_10.setEnabled(false);
					textField_10.setEditable(false);
					comboBox_8.setEditable(false);
					comboBox_8.setEnabled(false);
					comboBox_9.setEditable(false);
					comboBox_9.setEnabled(false);
					textField_13.setEditable(false);
					textField_13.setEnabled(false);
					textField_5.setEnabled(false);
					textField_5.setEditable(false);
					textField_6.setEnabled(false);
					textField_6.setEditable(false);
					textField_7.setEnabled(false);
					textField_7.setEditable(false);
					comboBox_6.setEditable(false);
					comboBox_6.setEnabled(false);
					comboBox_7.setEditable(false);
					comboBox_7.setEnabled(false);
					textField_12.setEditable(false);
					textField_12.setEnabled(false);
					l2AssociativityText.setEditable(false);
					l2AssociativityText.setEnabled(false);
					l2AssociativityText.setEditable(false);
					l2AssociativityText.setEnabled(false);
					
				}
				
			}
		});
		checkBox_1.setBounds(139, 41, 103, 23);
		panel_2.add(checkBox_1);
		
		checkBox_2 = new JCheckBox("L3");
		checkBox_2.setEnabled(false);
		checkBox_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if(((JCheckBox)e.getSource()).isSelected()){
					
					textField_8.setEnabled(true);
					textField_8.setEditable(true);
					textField_9.setEnabled(true);
					textField_9.setEditable(true);
					textField_10.setEnabled(true);
					textField_10.setEditable(true);
					comboBox_8.setEditable(true);
					comboBox_8.setEnabled(true);
					comboBox_9.setEditable(true);
					comboBox_9.setEnabled(true);
					textField_13.setEditable(true);
					textField_13.setEnabled(true);
					l3AssociativityText.setEditable(true);
					l3AssociativityText.setEnabled(true);
					
					
				}else{
					
					textField_8.setEnabled(false);
					textField_8.setEditable(false);
					textField_9.setEnabled(false);
					textField_9.setEditable(false);
					textField_10.setEnabled(false);
					textField_10.setEditable(false);
					comboBox_8.setEditable(false);
					comboBox_8.setEnabled(false);
					comboBox_9.setEditable(false);
					comboBox_9.setEnabled(false);
					textField_13.setEditable(false);
					textField_13.setEnabled(false);
					l3AssociativityText.setEditable(false);
					l3AssociativityText.setEnabled(false);
					
				}
				
			}
		});
		checkBox_2.setBounds(271, 41, 103, 23);
		panel_2.add(checkBox_2);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(10, 85, 28, 20);
		panel_2.add(textField_2);
		
		JLabel label_1 = new JLabel("S:");
		label_1.setBounds(10, 71, 28, 14);
		panel_2.add(label_1);
		
		JLabel label_2 = new JLabel("L:");
		label_2.setBounds(48, 71, 28, 14);
		panel_2.add(label_2);
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setBounds(48, 85, 28, 20);
		panel_2.add(textField_3);
		
		JLabel label_3 = new JLabel("M:");
		label_3.setBounds(86, 71, 28, 14);
		panel_2.add(label_3);
		
		textField_4 = new JTextField();
		textField_4.setColumns(10);
		textField_4.setBounds(85, 85, 28, 20);
		panel_2.add(textField_4);
		
		JLabel label_4 = new JLabel("S:");
		label_4.setBounds(139, 71, 28, 14);
		panel_2.add(label_4);
		
		textField_5 = new JTextField();
		textField_5.setEditable(false);
		textField_5.setEnabled(false);
		textField_5.setColumns(10);
		textField_5.setBounds(139, 85, 28, 20);
		panel_2.add(textField_5);
		
		textField_6 = new JTextField();
		textField_6.setEditable(false);
		textField_6.setEnabled(false);
		textField_6.setColumns(10);
		textField_6.setBounds(177, 85, 28, 20);
		panel_2.add(textField_6);
		
		textField_7 = new JTextField();
		textField_7.setEditable(false);
		textField_7.setEnabled(false);
		textField_7.setColumns(10);
		textField_7.setBounds(215, 85, 28, 20);
		panel_2.add(textField_7);
		
		JLabel label_5 = new JLabel("L:");
		label_5.setBounds(177, 71, 28, 14);
		panel_2.add(label_5);
		
		JLabel label_6 = new JLabel("M:");
		label_6.setBounds(215, 71, 28, 14);
		panel_2.add(label_6);
		
		JLabel label_7 = new JLabel("S:");
		label_7.setBounds(270, 71, 28, 14);
		panel_2.add(label_7);
		
		JLabel label_8 = new JLabel("L:");
		label_8.setBounds(308, 71, 28, 14);
		panel_2.add(label_8);
		
		JLabel label_9 = new JLabel("M:");
		label_9.setBounds(346, 71, 28, 14);
		panel_2.add(label_9);
		
		textField_8 = new JTextField();
		textField_8.setEditable(false);
		textField_8.setEnabled(false);
		textField_8.setColumns(10);
		textField_8.setBounds(270, 85, 28, 20);
		panel_2.add(textField_8);
		
		textField_9 = new JTextField();
		textField_9.setEditable(false);
		textField_9.setEnabled(false);
		textField_9.setColumns(10);
		textField_9.setBounds(308, 85, 28, 20);
		panel_2.add(textField_9);
		
		textField_10 = new JTextField();
		textField_10.setEditable(false);
		textField_10.setEnabled(false);
		textField_10.setColumns(10);
		textField_10.setBounds(346, 85, 28, 20);
		panel_2.add(textField_10);
		
		JCheckBox chckbxL_2 = new JCheckBox("L3");
		chckbxL_2.setBounds(270, 41, 103, 23);
		panel_2.add(chckbxL_2);
		
		JLabel lblWritePolicy = new JLabel("Hit write Policy :");
		lblWritePolicy.setBounds(10, 116, 85, 14);
		panel_2.add(lblWritePolicy);
		
		comboBox_4 = new JComboBox();
		comboBox_4.setModel(new DefaultComboBoxModel(new String[] {"Write Through", "Write Back"}));
		comboBox_4.setBounds(10, 130, 103, 23);
		panel_2.add(comboBox_4);
		
		JLabel lblMissWritePolicy = new JLabel("Miss write Policy :");
		lblMissWritePolicy.setBounds(10, 164, 103, 14);
		panel_2.add(lblMissWritePolicy);
		
		comboBox_5 = new JComboBox();
		comboBox_5.setModel(new DefaultComboBoxModel(new String[] {"Write Through", "Write Back"}));
		comboBox_5.setBounds(10, 180, 103, 23);
		panel_2.add(comboBox_5);
		
		JLabel label_11 = new JLabel("Hit write Policy :");
		label_11.setBounds(139, 116, 85, 14);
		panel_2.add(label_11);
		
		comboBox_6 = new JComboBox();
		comboBox_6.setEnabled(false);
		comboBox_6.setModel(new DefaultComboBoxModel(new String[] {"Write Through", "Write Back"}));
		comboBox_6.setBounds(139, 130, 103, 23);
		panel_2.add(comboBox_6);
		
		JLabel label_12 = new JLabel("Miss write Policy :");
		label_12.setBounds(139, 164, 103, 14);
		panel_2.add(label_12);
		
		comboBox_7 = new JComboBox();
		comboBox_7.setEnabled(false);
		comboBox_7.setModel(new DefaultComboBoxModel(new String[] {"Write Through", "Write Back"}));
		comboBox_7.setBounds(139, 180, 103, 23);
		panel_2.add(comboBox_7);
		
		JLabel label_10 = new JLabel("Hit write Policy :");
		label_10.setBounds(271, 116, 85, 14);
		panel_2.add(label_10);
		
		comboBox_8 = new JComboBox();
		comboBox_8.setEnabled(false);
		comboBox_8.setModel(new DefaultComboBoxModel(new String[] {"Write Through", "Write Back"}));
		comboBox_8.setBounds(271, 130, 103, 23);
		panel_2.add(comboBox_8);
		
		JLabel label_13 = new JLabel("Miss write Policy :");
		label_13.setBounds(271, 164, 103, 14);
		panel_2.add(label_13);
		
		comboBox_9 = new JComboBox();
		comboBox_9.setEnabled(false);
		comboBox_9.setModel(new DefaultComboBoxModel(new String[] {"Write Through", "Write Back"}));
		comboBox_9.setBounds(271, 180, 103, 23);
		panel_2.add(comboBox_9);
		
		JLabel lblNoOfAccess = new JLabel("Access time (in cycles):");
		lblNoOfAccess.setBounds(10, 214, 119, 14);
		panel_2.add(lblNoOfAccess);
		
		textField_11 = new JTextField();
		textField_11.setBounds(10, 231, 103, 20);
		panel_2.add(textField_11);
		textField_11.setColumns(10);
		
		JLabel label_14 = new JLabel("Access time (in cycles):");
		label_14.setBounds(139, 214, 119, 14);
		panel_2.add(label_14);
		
		textField_12 = new JTextField();
		textField_12.setEditable(false);
		textField_12.setEnabled(false);
		textField_12.setColumns(10);
		textField_12.setBounds(139, 231, 103, 20);
		panel_2.add(textField_12);
		
		JLabel label_15 = new JLabel("Access time (in cycles):");
		label_15.setBounds(271, 214, 119, 14);
		panel_2.add(label_15);
		
		textField_13 = new JTextField();
		textField_13.setEditable(false);
		textField_13.setEnabled(false);
		textField_13.setColumns(10);
		textField_13.setBounds(271, 231, 103, 20);
		panel_2.add(textField_13);
		
		JLabel lblHardwareOrganization = new JLabel("Hardware Organization :");
		lblHardwareOrganization.setBounds(10, 315, 119, 14);
		panel_2.add(lblHardwareOrganization);
		
		JLabel lblNumberOfWays = new JLabel("Number of ways :");
		lblNumberOfWays.setBounds(10, 338, 85, 14);
		panel_2.add(lblNumberOfWays);
		
		textField = new JTextField();
		textField.setBounds(139, 335, 103, 20);
		panel_2.add(textField);
		textField.setColumns(10);
		
		JLabel lblInstructionBuffers = new JLabel("Instruction buffer size :");
		lblInstructionBuffers.setBounds(10, 363, 119, 14);
		panel_2.add(lblInstructionBuffers);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(139, 360, 103, 20);
		panel_2.add(textField_1);
		
		JLabel lblNumberOfAdd = new JLabel("Number of ADD units :");
		lblNumberOfAdd.setBounds(10, 388, 119, 14);
		panel_2.add(lblNumberOfAdd);
		
		textField_14 = new JTextField();
		textField_14.setColumns(10);
		textField_14.setBounds(139, 385, 103, 20);
		panel_2.add(textField_14);
		
		JLabel lblNewLabel = new JLabel("Cycles :");
		lblNewLabel.setBounds(264, 388, 46, 14);
		panel_2.add(lblNewLabel);
		
		textField_15 = new JTextField();
		textField_15.setColumns(10);
		textField_15.setBounds(308, 385, 103, 20);
		panel_2.add(textField_15);
		
		JLabel lblNumberOfMul = new JLabel("Number of MUL units :");
		lblNumberOfMul.setBounds(10, 416, 119, 14);
		panel_2.add(lblNumberOfMul);
		
		textField_16 = new JTextField();
		textField_16.setColumns(10);
		textField_16.setBounds(139, 413, 103, 20);
		panel_2.add(textField_16);
		
		JLabel label_17 = new JLabel("Cycles :");
		label_17.setBounds(264, 416, 46, 14);
		panel_2.add(label_17);
		
		textField_17 = new JTextField();
		textField_17.setColumns(10);
		textField_17.setBounds(308, 413, 103, 20);
		panel_2.add(textField_17);
		
		JLabel lblNumberOfDiv = new JLabel("Number of DIV units :");
		lblNumberOfDiv.setBounds(10, 444, 119, 14);
		panel_2.add(lblNumberOfDiv);
		
		textField_18 = new JTextField();
		textField_18.setColumns(10);
		textField_18.setBounds(139, 441, 103, 20);
		panel_2.add(textField_18);
		
		JLabel label_19 = new JLabel("Cycles :");
		label_19.setBounds(264, 444, 46, 14);
		panel_2.add(label_19);
		
		textField_19 = new JTextField();
		textField_19.setColumns(10);
		textField_19.setBounds(308, 441, 103, 20);
		panel_2.add(textField_19);
		
		btnDone = new JButton("Done");
		btnDone.addActionListener(this);
		
		btnDone.setBounds(169, 528, 89, 23);
		panel_2.add(btnDone);
		
		lblNumberOfRob = new JLabel("Number of ROB entries :");
		lblNumberOfRob.setBounds(10, 475, 119, 14);
		panel_2.add(lblNumberOfRob);
		
		robText = new JTextField();
		robText.setColumns(10);
		robText.setBounds(139, 472, 103, 20);
		panel_2.add(robText);
		
		lblCacheOrganization = new JLabel("Associativity :");
		lblCacheOrganization.setBounds(10, 262, 119, 14);
		panel_2.add(lblCacheOrganization);
		
		l1AssociativityText = new JTextField();
		l1AssociativityText.setColumns(10);
		l1AssociativityText.setBounds(10, 275, 103, 20);
		panel_2.add(l1AssociativityText);
		
		JLabel lblCacheOrganization_1 = new JLabel("Associativity :");
		lblCacheOrganization_1.setBounds(139, 262, 103, 14);
		panel_2.add(lblCacheOrganization_1);
		
		JLabel lblAssociativity = new JLabel("Associativity :");
		lblAssociativity.setBounds(271, 262, 103, 14);
		panel_2.add(lblAssociativity);
		
		l2AssociativityText = new JTextField();
		l2AssociativityText.setEditable(false);
		l2AssociativityText.setEnabled(false);
		l2AssociativityText.setColumns(10);
		l2AssociativityText.setBounds(139, 275, 103, 20);
		panel_2.add(l2AssociativityText);
		
		l3AssociativityText = new JTextField();
		l3AssociativityText.setEnabled(false);
		l3AssociativityText.setEditable(false);
		l3AssociativityText.setColumns(10);
		l3AssociativityText.setBounds(271, 275, 103, 20);
		panel_2.add(l3AssociativityText);
		panel.setBounds(0, 0, 434, 562);
		frame.getContentPane().add(panel);
		panel.setLayout(null);
		
		instruction = new JComboBox();
		instruction.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				
				String inst = ((JComboBox)arg0.getSource()).getSelectedItem().toString();
				if(inst.equalsIgnoreCase("ADDI") || inst.equalsIgnoreCase("LW") || inst.equalsIgnoreCase("SW") || inst.equalsIgnoreCase("BEQ")){
					op2.setVisible(true);
					op3.setVisible(false);
					immediate.setVisible(true);
				}else{
					
					if(inst.equalsIgnoreCase("JMP")){
						
						op2.setVisible(false);
						op3.setVisible(false);
						immediate.setVisible(true);
						
					}else{
						
						if(inst.equalsIgnoreCase("RET")){
							
							op2.setVisible(false);
							op3.setVisible(false);
							immediate.setVisible(false);
							
						}else{
							
							if(inst.equalsIgnoreCase("JALR")){
								
								op2.setVisible(true);
								op3.setVisible(false);
								immediate.setVisible(false);
								
							}else{
								
								op2.setVisible(true);
								op3.setVisible(true);
								immediate.setVisible(false);
								
							}
							
						}
						
					}
					
				}
				
			}
		});
		instruction.setBounds(10, 29, 96, 20);
		panel.add(instruction);
		instruction.setModel(new DefaultComboBoxModel(new String[] {"ADD", "ADDI", "MUL", "DIV", "NAND", "BEQ", "JMP", "JALR", "RET", "LW", "SW"}));
		
		op1 = new JComboBox();
		op1.setBounds(111, 29, 51, 20);
		panel.add(op1);
		op1.setModel(new DefaultComboBoxModel(new String[] {"R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		
		op2 = new JComboBox();
		op2.setBounds(168, 29, 51, 20);
		panel.add(op2);
		op2.setModel(new DefaultComboBoxModel(new String[] {"R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		
		op3 = new JComboBox();
		op3.setBounds(228, 28, 51, 20);
		panel.add(op3);
		op3.setModel(new DefaultComboBoxModel(new String[] {"R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		
		immediate = new JTextField();
		immediate.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent arg0) {
				immediate.setText("");
			}
		});
		immediate.setVisible(false);
		immediate.setBounds(227, 28, 69, 20);
		panel.add(immediate);
		immediate.setText("IMMEDIATE");
		immediate.setColumns(10);
		
		JButton btnAddInstruction = new JButton("Add instruction");
		btnAddInstruction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				if(program.getText().equalsIgnoreCase("instructions"))
					program.setText("");
				
				String inst = instruction.getSelectedItem().toString();
				String fullInst = "";
				if(inst.equalsIgnoreCase("ADDI") || inst.equalsIgnoreCase("LW") || inst.equalsIgnoreCase("SW") || inst.equalsIgnoreCase("BEQ")){
					
					fullInst = " i" + numOfInst + ": " + inst + " "
							+ op1.getSelectedItem().toString() + " "
							+ op2.getSelectedItem().toString() + " "
							+ immediate.getText() + "\n";
					
					simulator.addInstruction(new Instruction(numOfInst, inst,
							op1.getSelectedIndex(),
							op2.getSelectedIndex(), 
							-1, 
							Integer.parseInt(immediate.getText())));
					numOfInst++;
					
				}else{
					
					if(inst.equalsIgnoreCase("JMP")){
						
						fullInst = " i" + numOfInst + ": " + inst + " "
								+ op1.getSelectedItem().toString() + " "
								+ immediate.getText() + "\n";
						
						simulator.addInstruction(new Instruction(numOfInst, inst, 
								op1.getSelectedIndex(), 
								-1, 
								-1, 
								Integer.parseInt(immediate.getText())));
						numOfInst++;
						
					}else{
						
						if(inst.equalsIgnoreCase("RET")){
							
							fullInst = " i" + numOfInst + ": " + inst + " "
									+ op1.getSelectedItem().toString() + "\n";
							
							simulator.addInstruction(new Instruction(numOfInst, inst, 
									op1.getSelectedIndex(), 
									-1, 
									-1, 
									0));
							numOfInst++;
							
						}else{
							
							if(inst.equalsIgnoreCase("JALR")){
								
								fullInst = " i" + numOfInst + ": " + inst + " "
										+ op1.getSelectedItem().toString()
										+ " "
										+ op2.getSelectedItem().toString()
										+ "\n";
								
								simulator.addInstruction(new Instruction(numOfInst, inst, 
										op1.getSelectedIndex(), 
										op2.getSelectedIndex(), 
										-1, 
										0));
								numOfInst++;
								
							}else{
								
								fullInst = " i" + numOfInst + ": " + inst + " "
										+ op1.getSelectedItem().toString()
										+ " "
										+ op2.getSelectedItem().toString()
										+ " "
										+ op3.getSelectedItem().toString()
										+ "\n";
								
								simulator.addInstruction(new Instruction(numOfInst, inst, 
										op1.getSelectedIndex(), 
										op2.getSelectedIndex(), 
										op3.getSelectedIndex(), 
										0));
								numOfInst++;
								
							}
							
						}
						
					}
					
				}
				
				program.setText(program.getText() + fullInst);
				
			}
		});
		btnAddInstruction.setBounds(317, 27, 107, 23);
		panel.add(btnAddInstruction);
		
		JButton btnSimulate = new JButton("Simulate");
		btnSimulate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				simulator.firstInstructionAddress = Integer.parseInt(startAddTextField.getText()); 
				simulator.simulate();
				
			}
		});
		btnSimulate.setBounds(159, 387, 96, 35);
		panel.add(btnSimulate);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 96, 414, 145);
		panel.add(scrollPane);
		
		program = new JTextArea();
		scrollPane.setViewportView(program);
		program.setText("Instructions");
		
		startAddTextField = new JTextField();
		startAddTextField.setBounds(193, 254, 86, 20);
		panel.add(startAddTextField);
		startAddTextField.setColumns(10);
		
		JLabel lblAddressOfFirst = new JLabel("Address of first instruction*");
		lblAddressOfFirst.setBounds(10, 252, 152, 25);
		panel.add(lblAddressOfFirst);
		
		JLabel lblAddData = new JLabel("Add Data:");
		lblAddData.setBounds(10, 288, 69, 14);
		panel.add(lblAddData);
		
		JLabel lblAddress = new JLabel("Address");
		lblAddress.setBounds(10, 313, 46, 14);
		panel.add(lblAddress);
		
		addressTextField = new JTextField();
		addressTextField.setBounds(66, 310, 86, 20);
		panel.add(addressTextField);
		addressTextField.setColumns(10);
		
		JLabel lblValue = new JLabel("Value");
		lblValue.setBounds(193, 313, 46, 14);
		panel.add(lblValue);
		
		valueTextField = new JTextField();
		valueTextField.setBounds(228, 310, 86, 20);
		panel.add(valueTextField);
		valueTextField.setColumns(10);
		
		JButton btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				//add to the memoryyy----------------------------------------------------------------------------------------------<<
				addressTextField.setText("");
				valueTextField.setText("");
				
			}
		});
		btnAdd.setBounds(345, 309, 79, 23);
		panel.add(btnAdd);
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnDone){
			
			int s,l,m;
			String hitWritePolicy;
			String missWritePolicy;
			int noOfcycles;
			
			if(checkBox.isSelected()){
				s = Integer.parseInt(textField_2.getText());
				l = Integer.parseInt(textField_3.getText());
				m = Integer.parseInt(textField_4.getText());
				hitWritePolicy = comboBox_4.getSelectedItem().toString();
				missWritePolicy = comboBox_5.getSelectedItem().toString();
				noOfcycles = Integer.parseInt(textField_11.getText());
				
				cache = new Cache(s, l, m, hitWritePolicy, missWritePolicy, noOfcycles);
				cache.setAssociativity(Integer.parseInt(l1AssociativityText.getText()));
				simulator.caches.add(cache);
			}
			
			if(checkBox_1.isSelected()){
				s = Integer.parseInt(textField_5.getText());
				l = Integer.parseInt(textField_6.getText());
				m = Integer.parseInt(textField_7.getText());
				hitWritePolicy = comboBox_6.getSelectedItem().toString();
				missWritePolicy = comboBox_7.getSelectedItem().toString();
				noOfcycles = Integer.parseInt(textField_12.getText());
				
				cache = new Cache(s, l, m, hitWritePolicy, missWritePolicy, noOfcycles);
				cache.setAssociativity(Integer.parseInt(l2AssociativityText.getText()));
				simulator.caches.add(cache);
			}
			
			if(checkBox_2.isSelected()){
				s = Integer.parseInt(textField_8.getText());
				l = Integer.parseInt(textField_9.getText());
				m = Integer.parseInt(textField_10.getText());
				hitWritePolicy = comboBox_8.getSelectedItem().toString();
				missWritePolicy = comboBox_9.getSelectedItem().toString();
				noOfcycles = Integer.parseInt(textField_13.getText());
				
				cache = new Cache(s, l, m, hitWritePolicy, missWritePolicy, noOfcycles);
				cache.setAssociativity(Integer.parseInt(l3AssociativityText.getText()));
				simulator.caches.add(cache);
			}
			simulator.ways = Integer.parseInt(textField.getText());
			simulator.bufferSize = Integer.parseInt(textField_1.getText());
			simulator.addUnits = Integer.parseInt(textField_14.getText());
			simulator.addReservationStation = new Reservation_Station_Entry[simulator.addUnits + 3];// the 3 are for the other instructions.
			simulator.mulUnits = Integer.parseInt(textField_16.getText());
			simulator.multiplyReservationStation = new Reservation_Station_Entry[simulator.mulUnits];
			simulator.divUnits = Integer.parseInt(textField_18.getText());
			simulator.divideReservationStation = new Reservation_Station_Entry[simulator.divUnits];
			simulator.addCycles = Integer.parseInt(textField_15.getText());
			simulator.mulCycles = Integer.parseInt(textField_17.getText());
			simulator.divCycles = Integer.parseInt(textField_19.getText());
			simulator.robSize = Integer.parseInt(robText.getText());
			
			panel_2.setVisible(false);
			panel.setVisible(true);
			
		}
		
		
		
	}
}
